﻿using System;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.UI;
using Controllers;

namespace Web
{
    public partial class ExamWelcome : System.Web.UI.Page
    {
        protected ExamType examType;
        protected int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = int.Parse(Request.QueryString["id"]);
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            examType = ctx.ExamTypes.Single(r => r.Id == id);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if(examType.Testers.IndexOf(ebi.Id.ToString())<0)
            {
                // Define the name and type of the client script on the page.
                String csName = "WarningScript";
                Type csType = this.GetType();

                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager cs = Page.ClientScript;

                // Check to see if the client script is already registered.
                if (!cs.IsClientScriptBlockRegistered(csType, csName))
                {
                    StringBuilder csText = new StringBuilder();
                    csText.Append(@"<script>alert('您未被授权进行该场考试,页面将被关闭');window.close();</script>");
                    cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
                }
            }
        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            if(examType.StartTime>DateTime.Now||examType.EndTime<DateTime.Now)
            {
                // Define the name and type of the client script on the page.
                String csName = "WarningScript";
                Type csType = this.GetType();

                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager cs = Page.ClientScript;

                // Check to see if the client script is already registered.
                if (!cs.IsClientScriptBlockRegistered(csType, csName))
                {
                    StringBuilder csText = new StringBuilder();
                    csText.Append(@"<script>alert('考试时间 未到/已过');</script>");
                    cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
                    Response.End();
                }
            }
            else
            {
                Response.Redirect("Examination.aspx?typeId="+id);
            }
        }
    }
}